// JsonConverter.kt
package com.srtp.secureprotocol.util

import com.srtp.secureprotocol.serializable.req.Bundle
import com.srtp.secureprotocol.serializable.req.PreKeysRequest
import com.srtp.secureprotocol.serializable.resp.*
import org.json.JSONArray
import org.json.JSONObject

object JsonConverter {
    fun convertBundleToJson(bundle: Bundle): JSONObject {
        return JSONObject().apply {
            put("localId", bundle.localId)
            put("preKeys", convertListToJsonArray(bundle.preKeys) { key ->
                JSONObject().apply {
                    put("id", key.id)
                    put("publicKey", key.publicKey)
                    put("cipher", key.cipher)
                    put("salt", key.salt)
                }
            })
            put("senderKeys", convertListToJsonArray(bundle.senderKeys) { key ->
                JSONObject().apply {
                    put("key", key.key)
                    put("id", key.id)
                }
            })
            put("signedPreKeys", convertListToJsonArray(bundle.signedPreKeys) { key ->
                JSONObject().apply {
                    put("id", key.id)
                    put("publicKey", key.publicKey)
                    put("signature", key.signature)
                    put("cipher", key.cipher)
                    put("salt", key.salt)
                    put("timestamp", key.timestamp)
                }
            })
            put("identityKeys", convertListToJsonArray(bundle.identityKeys) { key ->
                JSONObject().apply {
                    put("id", key.id)
                    put("publicKey", key.publicKey)
                    put("cipher", key.cipher)
                    put("salt", key.salt)
                    put("timestamp", key.timestamp)
                }
            })
        }
    }

    fun parseIdentityKeyToJson(key: IdentityKey): JSONObject {
        return JSONObject().apply {
            put("id", key.id)
            put("publicKey", key.publicKey)
            put("cipher", key.cipher)
            put("salt", key.salt)
            put("timestamp", key.timestamp)
        }
    }

    fun parseSignedPreKeyToJson(key: SignedPreKey): JSONObject {
        return JSONObject().apply {
            put("id", key.id)
            put("publicKey", key.publicKey)
            put("signature", key.signature)
            put("cipher", key.cipher)
            put("salt", key.salt)
            put("timestamp", key.timestamp)
        }
    }

    fun convertPreKeysToJson(preKeys: List<PreKey>): JSONArray {
        return convertListToJsonArray(preKeys) { key ->
            JSONObject().apply {
                put("id", key.id)
                put("publicKey", key.publicKey)
                put("cipher", key.cipher)
                put("salt", key.salt)
            }
        }
    }

    // JSONArray 转 PreKey 列表
    fun parsePreKeys(jsonArray: JSONArray): List<PreKey> {
        return (0 until jsonArray.length()).map { index ->
            val keyObj = jsonArray.getJSONObject(index)
            PreKey(
                id = keyObj.getInt("id"),
                publicKey = keyObj.getString("publicKey"),
                cipher = keyObj.getString("cipher"),
                salt = keyObj.getString("salt")
            )
        }
    }

    fun <T> convertListToJsonArray(
        list: List<T>,
        converter: (T) -> JSONObject
    ): JSONArray {
        val array = JSONArray()
        list.forEach { item ->
            array.put(converter(item))
        }
        return array
    }

    fun parseIdentityKey(json: JSONObject): IdentityKey {
        return IdentityKey(
            id = json.getInt("id"),
            publicKey = json.getString("publicKey"),
            cipher = json.getString("cipher"),
            salt = json.getString("salt"),
            timestamp = json.getString("timestamp")
        )
    }

    fun parseSignedPreKey(json: JSONObject): SignedPreKey {
        return SignedPreKey(
            id = json.getInt("id"),
            publicKey = json.getString("publicKey"),
            signature = json.getString("signature"),
            cipher = json.getString("cipher"),
            salt = json.getString("salt"),
            timestamp = json.getString("timestamp")
        )
    }
}